<?php

namespace App\Jobs;

use App\Services\Log;
use App\Services\PushService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Redis;

class PushQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $push;

    /**
     * 创建任务实例
     *
     * @param  Podcast  $podcast
     */
    public function __construct($push)
    {
        $this->push = $push;
    }

    /**
     * 执行任务
     *
     * @param  AudioProcessor  $processor
     * @return void
     */
    public function handle()
    {
        $error = [];
        $pushData = $this->push;
        if(!empty($pushData)){
            $pushParams = $pushData['data'];
            if(!isset($pushData['type']))
                return ['status'=>'error', 'message'=>'参数异常'];
            $action = $pushData['type'];

            $data = $this->$action($pushData['type'], $pushParams);
            // 处理异常
            if($data['status'] == 'error')
                Redis::lpush('push_error_queue', json_encode(['dispose'=>$data, 'org'=>$pushData]));
        }
    }

    /*
     * 单个推送
     * */
    public function singlePush($type, $pushParams)
    {
        $error = [];
        // 检查基础参数
        if(!isset($pushParams['channel_id']) || !isset($pushParams['title']) ||  !isset($pushParams['content']) ||  !isset($pushParams['description'])
            || !isset($pushParams['msg_type']) || !isset($pushParams['notify_id']) || !isset($pushParams['notify_foreground']))
            $error[] = '参数异常';
        if(!isset($pushParams['mobile_list']))
            $error[] = '未知推送用户';

        // 执行推送
        if(empty($error)){
            $pushObj = new PushService();

            // 区分应用渠道推送
            $data = $pushObj->initInstance(['channel_id'=>$pushParams['channel_id']]);

            $post_params['title'] = $pushParams['title'];
            $post_params['content'] = $pushParams['content'];
            $post_params['description'] = $pushParams['description'];
            // 消息类型：1通知，0透传
            $post_params['msg_type'] = $pushParams['msg_type'];
            // 发送业务id ，id的作用，同一重复发送覆盖，具体结合推送平台文档操作
            $post_params['notify_id'] = $pushParams['notify_id'];
            // 可选项
            // 开启/关闭app在前台时的通知弹出 1:弹出通知  0 不弹出
            $post_params['notify_foreground'] = $pushParams['notify_foreground'];
            if(isset($pushParams['bi_tag']) && !empty($pushParams['bi_tag']))
                $post_params['bi_tag'] = $pushParams['bi_tag'];
            if(isset($pushParams['icon']) && !empty($pushParams['icon']))
                $post_params['icon'] = $pushParams['icon'];
            if(isset($pushParams['extend']) && !empty($pushParams['extend']))
                $post_params['extend'] = $pushParams['extend'];
            $post_params['mobile_list'] = $pushParams['mobile_list'];

            $data = $pushObj->$type($post_params);
            if($data['status'] == 'error')
                $error[] = $data['message'];
        }

        if(!empty($error))
            return ['status'=>'error', 'message'=>implode(',', $error)];
        return ['status'=>'success', 'message'=>'执行成功'];
    }
}
